package configs.aspose;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.ceba.base.helper.LoggerHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aspose.pdf.Document;
import com.aspose.pdf.devices.JpegDevice;
import com.aspose.pdf.devices.Resolution;
import com.aspose.words.License;
import com.ceba.forpdf.pdf.PdfTools;
import com.ceba.test.base.configs.CebaConfig;

import abc.util.FileHelper;

@SuppressWarnings("deprecation")
public class AsposeUtils {
	static Logger logger = LoggerFactory.getLogger(AsposeUtils.class);
	private static InputStream license;
	/**
	 * 获取license
	 * 
	 * @return
	 */
	public static boolean getAsposePdfLicense() {
		boolean result = false;
		try {
			license = AsposeUtils.class.getResourceAsStream("aspose_pdf_license.xml");// license路径
			License aposeLic = new License();
			aposeLic.setLicense(license);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public int changePdfToImg(int pageNum,String source,String des){
		Document pdfDocument = null;
		OutputStream imageStream = null;
		try {
			if(!getAsposePdfLicense()){
				return 1;
			}
			// Open document
			pdfDocument = new Document(source);

			// Loop through all the pages of PDF file
			for (int pageCount = 1; pageCount <= 1; pageCount++) {
				// Create stream object to save the output image
				imageStream = new FileOutputStream(des);

				// Create Resolution object
				Resolution resolution = new Resolution(300);
				// Create JpegDevice object where second argument indicates the
				// quality of resultant image
				JpegDevice jpegDevice = new JpegDevice(resolution, 100);
				// Convert a particular page and save the image to stream
				jpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);

				// Close the stream
				imageStream.close();
			}
		} catch (FileNotFoundException e) {
			LoggerHelper.error(logger, e, "");
			return 2;
		} catch (IOException e) {
			LoggerHelper.error(logger, e, "");
			return 100;
		} finally {
			if(pdfDocument !=null ){
				pdfDocument.close();
			}
			if(imageStream !=null){
				try {
					imageStream.close();
				} catch (IOException e) {
					LoggerHelper.error(logger, e, "");
					return 100;
				}
			}
		}
		return 0;
	}
	
	/**
	 * 把office文档转PDF
	 * @param officePath
	 * @param pdfPath
	 * @return
	 */
	public static boolean officeToPdf(String officePath,String pdfPath){
		try {
			if(CebaConfig.PDF_CREATE_MODE.toLowerCase().equals("adobe")){
				if(officeToPdfByAdobe(officePath, pdfPath)){
					return true;
				}
				else {
					logger.debug("adobe工具调用出错");
					System.out.println("adobe工具调用出错");
					return officeToPdfByAspose(officePath, pdfPath);
				}
			}
			else if (CebaConfig.PDF_CREATE_MODE.toLowerCase().equals("office")) {
				if(officeToPdfByOffice(officePath, pdfPath)){
					return true;
				}
				else {
					logger.debug("office工具调用出错");
					System.out.println("office工具调用出错");
					return officeToPdfByAspose(officePath, pdfPath);
				}
			}
			else {
				return officeToPdfByAspose(officePath, pdfPath);
			}
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
			return false;
		}
	}
	
	/**
	 * 使用Adobe把office文档转PDF
	 * @param officePath
	 * @param pdfPath
	 * @return
	 */
	private static boolean officeToPdfByAdobe(String officePath,String pdfPath) {
        try {
        	pdfPath = PdfTools.officeToPdfByAdobe(officePath,pdfPath);
        } catch (Exception e) {
        	LoggerHelper.error(logger, e, "");
        	return false;
        }   
        if(FileHelper.exists(pdfPath)){
       	 	return true;
        }
		return false;
	}
	
	private static boolean officeToPdfByAspose(String officePath,String pdfPath) {
		try {
			pdfPath = PdfTools.officeToPdfByAspose(officePath, pdfPath);
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
			return false;
		}
		if(FileHelper.exists(pdfPath)){
       	 	return true;
        }
		return false;
	}
	
	/**
	 * 使用offic把office文档转PDF
	 * @param officePath
	 * @param pdfPath
	 * @return
	 */
	private static boolean officeToPdfByOffice(String officePath,String pdfPath) {
		String extName=FileHelper.getFileExtName(officePath).toLowerCase();
		try {
			pdfPath = PdfTools.officeToPdfByOffice(officePath,pdfPath,extName);
        } catch (Exception e) {
        	LoggerHelper.error(logger, e, "");
        } 
		if(FileHelper.exists(pdfPath)){
       	 	return true;
        }		
		return false;
	}
}
